
//  CZMainViewController.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  越早执行约好, 一般会放到appDelegate里面
        //UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        // 添加子视图控制器的两种方式
//        viewControllers = [xx,]
        
//        addChildViewController(xx)
        //  指定tabbar
        let czTabbar = CZTabBar()
        //  设置代理对象
        czTabbar.czDelegate = self

        //  传入指定闭包
        czTabbar.composeButtonClosure = { [weak self] in
            
            if !CZUserAccountViewModel.shareUserAccount.isLogin {
                SVProgressHUD.showErrorWithStatus("请先登录,亲~")
                return
            }
            //  代码执行到此表示已经登录
            if let weakSelf = self {
                let composeView = CZComposeView()
                composeView.show(weakSelf)
            }
            
//            //  获取最上面的一个window
//            let window = UIApplication.sharedApplication().windows.last!
//            
//            window.addSubview(composeView)
            
            
            
        }
        //  使用kvc给只读属性设置值
        setValue(czTabbar, forKey: "tabBar")
        
        //self.tabBar = czTabbar
        
        addChildViewController(CZHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(CZMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(CZDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(CZProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    //  重载addChildViewController
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //  设置自定义tabBarItem
        childController.tabBarItem = CZTabBarItem()
        //  设置tabar的文字和显示图片
       // childController.tabBarItem.title = title
       // childController.navigationItem.title = title
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        //childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //  设置图片位置
        //childController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        //  指定渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //  设置tabbar字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //  设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        
        //  添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
    
        addChildViewController(nav)
        
        
        
    }

   

}


extension CZMainViewController: CZTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }

}













